#include "prod.hpp"
#include <string>
#include<unistd.h>

RingQueue<string> BQ;

// 生产
void *prod(void *name)
{
    int n = 100;
    while (n--)
    {
        
        string s = (const char *)name;
        string in = s + "生产的数据";
        BQ.Push(in);
        cout << s << "生产了数据" << endl;
    }
    return nullptr;
}

void *cons(void *name)
{
    int n = 100;
    while (n--)
    {
        sleep(2);
        string s = (const char *)name;
        string out;
        BQ.Pop(out);
        cout << s << "消费了: " << out << endl;
    }
    return nullptr;
}

int main()
{
    pthread_t p1;
    pthread_t p2;
    pthread_t p3;
    pthread_t c1;
    pthread_t c2;
    pthread_t c3;

    pthread_create(&p1, nullptr, prod, (void *)"1号生产者");
    pthread_create(&p2, nullptr, prod, (void *)"2号生产者");
    pthread_create(&p3, nullptr, prod, (void *)"3号生产者");

    pthread_create(&c1, nullptr, cons, (void *)"1号消费者");
    pthread_create(&c2, nullptr, cons, (void *)"2号消费者");
    pthread_create(&c3, nullptr, cons, (void *)"3号消费者");

    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);

    return 0;
}